package com.logistics.user;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.jstl.sql.Result;

import com.logistics.database.Distributiondatabase;
import com.logistics.database.Userdatabase;
import com.logistics.distribution.Product;
import com.logistics.entity.User;

public class Registerservlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public Registerservlet() {
		super();
	}

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		   String type = request.getParameter("type");       
		   Userdatabase ddb=new Userdatabase();

	       if(type.equals("insert"))
	       {
	    	   String user_name=new String(request.getParameter("user_name").getBytes("iso-8859-1"),"utf-8");
	    	   String password=new String(request.getParameter("password").getBytes("iso-8859-1"),"utf-8");
	    	   String user_nick=new String(request.getParameter("user_nick").getBytes("iso-8859-1"),"utf-8");
	    	   String id_card=new String(request.getParameter("id_card").getBytes("iso-8859-1"),"utf-8");
	    	   String company=new String(request.getParameter("company").getBytes("iso-8859-1"),"utf-8");
	    	   String cellphone=new String(request.getParameter("cellphone").getBytes("iso-8859-1"),"utf-8");
	    	   String tel=new String(request.getParameter("tel").getBytes("iso-8859-1"),"utf-8");
	    	   String email=new String(request.getParameter("email").getBytes("iso-8859-1"),"utf-8");
	    	   String address=new String(request.getParameter("address").getBytes("iso-8859-1"),"utf-8");
	    	   String zipcode=new String(request.getParameter("zipcode").getBytes("iso-8859-1"),"utf-8");
	    	   
	    	   if(company==null)
	    		   company="";
	    	   if(tel==null)
	    		   tel="";
	    	   if(address==null)
	    		   address="";
	    	   if(zipcode==null)
	    		   zipcode="";
	    	   ddb.userInsert2(user_name, password, user_nick,id_card, company,tel,cellphone,address,zipcode,email);
	    	   
	       }
  
	      /* ArrayList<User> users=new ArrayList<User>();
	       ResultSet rs=ddb.userQuery();
	       User ur=new User();
	       try {
	    	   while(rs.next())
			   {
				  
				   ur.setUser_name(rs.getString(1));
				   ur.setId_card(rs.getString(2));
				   ur.setCompany(rs.getString(3));
				   ur.setTel(rs.getString(4));
				   ur.setCellphone(rs.getString(5));
				   ur.setAddress(rs.getString(6));
				   ur.setZipcode(rs.getString(7));
				   ur.setEmail(rs.getString(8));
				   ur.setPassword(rs.getString(9));
				   ur.setUser_nick(rs.getString(10));
	 
				   users.add(ur);
			   }
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	       request.setAttribute("users", users);
	*/
	       //request.getRequestDispatcher("../login.jsp").forward(request, response); 
	       response.sendRedirect("../login.jsp");
	       
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request,response);
	}

}
